package News.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import News.entity.Comments;
import News.util.DBUtil;

public class CommentDao {
	public int add(Comments s) {
		String sql = "insert INTO Comments (cnid,ccontent,cdate,cip,cauthor) VALUES(?,?,?,?,?)";
		return DBUtil.executeUpdate(sql, s.getCnid(), s.getCcontent(), s.getCdate(), s.getCip(), s.getCauthor());
	}

	public List<Comments> finComments(int newsid) {
		String sql = "select *from Comments where cnid=?";
		List<Comments> dates = new ArrayList<Comments>();
		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, newsid);
			rs = ps.executeQuery();
			while (rs.next()) {
				Comments s = new Comments();
				s.setCnid(rs.getInt("cnid"));
				s.setCcontent(rs.getString("ccontent"));
				s.setCdate(rs.getString("cdate"));
				s.setCip(rs.getString("cip"));
				s.setCauthor(rs.getString("cauthor"));
				dates.add(s);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, ps, conn);
		}
		return dates;
	}
}
